<?php
class Portfolio extends AppModel
{
    var $name = 'Portfolio';
    var $belongsTo = 'Currency, User';
        
    function createPortfolioForUser($userId)
    {
    	$portfolio = false;
    	
    	$user = $this->User->findById($userId);
    	if(!empty($user))
	    {
			$defaultCurrency = $this->Currency->defaultCurrency();
	    	
	    	//Create a new portfolio
	    	$portfolioData = array('user_id' => $userId, 'currency_id' => $defaultCurrency['Currency']['id']);
	    	
	    	if ($this->create($portfolioData, true)) //Set filterKey to true. Stops faked ids updating existing portfolio
			{
				if($this->validates())
				{
					$portfolio = $this->save(NULL, false, array('user_id', 'currency_id')); //The whitelist does not cover the id field.
					if($portfolio)
					{
						$portfolio = $this->findById($this->id);
					}
				}
			}
    	}
    	
		return $portfolio;
    }
}
?>